package com.ushine.web.component.validation.rule;

import com.ushine.web.component.validation.annotation.IdCard;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * @Author: 邱鹏
 * @Date 2021-01-14 14:54
 * @CompileVersion 1.8
 * @Version 1.0
 */
@Slf4j
public class IdCardValidator implements ConstraintValidator<IdCard, String> {
    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        return strongVerifyIdNumber(s);
    }

    /**
     * 大陆地区地域编码最大值
     **/
    public static final int MAX_MAINLAND_AREA_CODE = 659004;
    /**
     * 大陆地区地域编码最小值
     **/
    public static final int MIN_MAINLAND_AREA_CODE = 110000;
    /**
     * 香港地域编码值
     **/
    public static final int HONG_KONG_AREA_CODE = 810000;
    /**
     * 台湾地域编码值
     **/
    public static final int TAIWAN_AREA_CODE = 710000;
    /**
     * 澳门地域编码值
     **/
    public static final int MACAO_AREA_CODE = 820000;

    /**
     * 数字正则
     **/
    private static final String REGEX_NUM = "^[0-9]*$";
    /**
     * 闰年生日正则
     **/
    private static final String REGEX_BIRTHDAY_IN_LEAP_YEAR = "^((19[0-9]{2})|(200[0-9])|(201[0-5]))((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))$";
    /**
     * 平年生日正则
     **/
    private static final String REGEX_BIRTHDAY_IN_COMMON_YEAR = "^((19[0-9]{2})|(200[0-9])|(201[0-5]))((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))$";

    /**
     * 身份证长度
     */
    private static final int ID_LENGTH = 18;

    private static final HashSet<String> BLACK_SET = new HashSet<String>() {

        private static final long serialVersionUID = 48136604486603324L;

        {
            add("111111111111111");
        }
    };

    /**
     * <p>
     * 身份证格式强校验
     * </p>
     * <p>
     * 1、号码的结构 公民身份号码是特征组合码，由十七位数字本体码和一位校验码组成。排列顺序从左至右依次为：六位数字地址码，
     * 八位数字出生日期码，三位数字顺序码和一位数字校验码。
     * </p>
     * <p>
     * 2、地址码(前六位数）表示编码对象常住户口所在县(市、旗、区)的行政区划代码，按GB/T2260的规定执行。
     * </p>
     * <p>
     * 3、出生日期码（第七位至十四位）表示编码对象出生的年、月、日，按GB/T7408的规定执行，年、月、日代码之间不用分隔符。
     * </p>
     * <p>
     * 4、顺序码（第十五位至十七位）表示在同一地址码所标识的区域范围内，对同年、同月、同日出生的人编定的顺序号， 顺序码的奇数分配给男性，偶数分配给女性。
     * </p>
     * <p>
     * 5、校验码（第十八位数）
     * （1）十七位数字本体码加权求和公式 S = Sum(Ai * Wi), i = 0, ... , 16 ，先对前17位数字的权求和
     * Ai:表示第i位置上的身份证号码数字值 Wi:表示第i位置上的加权因子 Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4
     * 2 （2）计算模 Y = mod(S, 11) （3）通过模得到对应的校验码 Y: 0 1 2 3 4 5 6 7 8 9 10 校验码: 1 0
     * X 9 8 7 6 5 4 3 2
     * </p>
     */
    public final boolean strongVerifyIdNumber(String idNumber) {
        int subMinIndex = 6;
        int subMaxIndex = 6;
        if (StringUtils.isBlank(idNumber)) {
            return false;
        }
        idNumber = idNumber.trim();

        if (BLACK_SET.contains(idNumber)) {
            return false;
        }
        if (!checkIdNumberRegex(idNumber)) {
            return false;
        }
        if (!checkIdNumberArea(idNumber.substring(0, subMinIndex))) {
            return false;
        }
        idNumber = convertFifteenToEighteen(idNumber);
        if (!checkBirthday(idNumber.substring(subMinIndex, subMaxIndex))) {
            return false;
        }
        return checkIdNumberVerifyCode(idNumber);
    }

    /**
     * 身份证正则校验
     */
    private boolean checkIdNumberRegex(String idNumber) {
        return Pattern.matches("^([0-9]{17}[0-9Xx])$|^([0-9]{15})$", idNumber);
    }

    /**
     * 身份证地区码检查
     */
    private boolean checkIdNumberArea(String idNumberArea) {
        int areaCode = Integer.parseInt(idNumberArea);
        if (areaCode == HONG_KONG_AREA_CODE || areaCode == MACAO_AREA_CODE || areaCode == TAIWAN_AREA_CODE) {
            return true;
        }
        return areaCode <= MAX_MAINLAND_AREA_CODE && areaCode >= MIN_MAINLAND_AREA_CODE;
    }

    /**
     * 将15位身份证转换为18位
     */
    private String convertFifteenToEighteen(String idNumber) {
        int idMinSize = 15;
        if (idMinSize != idNumber.length()) {
            return idNumber;
        }
        idNumber = idNumber.substring(0, 6) + "19" + idNumber.substring(6, 15);
        idNumber = idNumber + getVerifyCode(idNumber);
        return idNumber;
    }

    /**
     * 根据身份证前17位计算身份证校验码
     */
    private String getVerifyCode(String idNumber) {
        if (!Pattern.matches(REGEX_NUM, idNumber.substring(0, ID_LENGTH - 1))) {
            return null;
        }
        String[] valCodeArr = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
        String[] wi = {"7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2"};

        int sum = 0;
        for (int i = 0; i < ID_LENGTH - 1; i++) {
            sum = sum + Integer.parseInt(String.valueOf(idNumber.charAt(i))) * Integer.parseInt(wi[i]);
        }
        return valCodeArr[sum % 11];
    }

    /**
     * 身份证出生日期嘛检查
     */
    private boolean checkBirthday(String idNumberBirthdayStr) {
        Integer year = null;
        try {
            year = Integer.valueOf(idNumberBirthdayStr.substring(0, 4));
        } catch (Exception e) {
            log.info("exception:", e);
        }
        if (null == year) {
            return false;
        }
        if (isLeapYear(year)) {
            return Pattern.matches(REGEX_BIRTHDAY_IN_LEAP_YEAR, idNumberBirthdayStr);
        } else {
            return Pattern.matches(REGEX_BIRTHDAY_IN_COMMON_YEAR, idNumberBirthdayStr);
        }
    }

    /**
     * 判断是否为闰年
     */
    private boolean isLeapYear(int year) {
        return (year % 400 == 0) || (year % 100 != 0 && year % 4 == 0);
    }

    /**
     * 身份证校验码检查
     */
    private boolean checkIdNumberVerifyCode(String idNumber) {
        if (StringUtils.isNotEmpty(idNumber)) {
            return Objects.requireNonNull(getVerifyCode(idNumber)).equalsIgnoreCase(idNumber.substring(17));
        }
        return false;
    }

}
